package com.hcxdi.calanderapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.hcxdi.calanderapp.R;
import com.hcxdi.calanderapp.bean.DayBean;
import com.hcxdi.calanderapp.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Calendar;

public class CalendarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ArrayList<DayBean> list = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private Context context;
    private String startTitle;
    private String endTitle;

    public CalendarAdapter(Context context,String startTitle,String endTitle) {
        this.context=context;
        this.startTitle=startTitle;
        this.endTitle=endTitle;

    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    public ArrayList<DayBean> getData(){
        return list==null?new ArrayList<>():list;
    }

    public void setData(ArrayList<DayBean> list){
        this.list=list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == DayBean.type_day) {
            View rootView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_day, viewGroup, false);

            final CalendarAdapter.DayViewHolder dayViewHolder = new CalendarAdapter.DayViewHolder(rootView);
            dayViewHolder.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null && dayViewHolder.itemDay.isEnabled()) {
                    onItemClickListener.onItemClick(v, dayViewHolder.getLayoutPosition());
                }
            });
            return dayViewHolder;
        } else if (i == DayBean.type_month) {
            View rootView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_month, viewGroup, false);
            final CalendarAdapter.MonthViewHolder monthViewHolder = new CalendarAdapter.MonthViewHolder(rootView);
            monthViewHolder.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, monthViewHolder.getLayoutPosition());
                }
            });
            return monthViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        DayBean bean = list.get(i);
        if (viewHolder instanceof CalendarAdapter.MonthViewHolder) {
            ((MonthViewHolder) viewHolder).tvMonth.setText(bean.getMonthStr());
        } else {
            CalendarAdapter.DayViewHolder vh = ((CalendarAdapter.DayViewHolder) viewHolder);
            vh.tvDay.setText(bean.getDay());
            vh.tvLunar.setText(bean.getLunar());
            if (bean.getItemState() == DayBean.STATE_BEGIN_DATE || bean.getItemState() == DayBean.STATE_END_DATE) {
                //开始日期或结束日期
                vh.itemDay.setBackgroundResource(R.drawable.circle_red);
                vh.tvDay.setTextColor(ContextCompat.getColor(context,R.color.white));
                if (bean.getItemState() == DayBean.STATE_END_DATE) {
                    vh.tvLunar.setText(endTitle);
                } else {
                    vh.tvLunar.setText(startTitle);
                }
                vh.tvLunar.setTextColor(ContextCompat.getColor(context,R.color.white));

            } else if (bean.getItemState() == DayBean.STATE_SELECTED) {
                //选中状态
                vh.itemDay.setBackgroundResource(R.drawable.circle_red_shallow);
                vh.tvDay.setTextColor(ContextCompat.getColor(context,R.color.white));
                vh.tvLunar.setTextColor(ContextCompat.getColor(context,R.color.white));
            } else if (bean.getItemState() == DayBean.STATE_CHOOSE) {
                //已被预订状态
                vh.itemDay.setBackgroundResource(R.drawable.circle_red_shallow);
                vh.tvDay.setTextColor(ContextCompat.getColor(context,R.color.white));
                vh.tvLunar.setTextColor(ContextCompat.getColor(context,R.color.white));
                vh.itemDay.setEnabled(false);
            } else {
                //正常状态
                vh.itemView.setBackgroundColor(ContextCompat.getColor(context,R.color.white));
                if (bean.getDate() != null) {
                    //过去的时间
                    if (TimeUtils.isOldDate(Calendar.getInstance().getTime(),bean.getDate())){
                        vh.tvDay.setTextColor(ContextCompat.getColor(context,R.color.gray));
                        vh.tvLunar.setTextColor(ContextCompat.getColor(context,R.color.gray));
                        vh.itemDay.setEnabled(false);
                    }else {
                        vh.itemDay.setEnabled(true);
                        if (bean.getDay().equals("今天")){
                            vh.tvDay.setTextColor(ContextCompat.getColor(context, R.color.red));
                            vh.tvLunar.setTextColor(ContextCompat.getColor(context, R.color.red));
                        }else {
                            vh.tvDay.setTextColor(ContextCompat.getColor(context, R.color.black));
                            vh.tvLunar.setTextColor(ContextCompat.getColor(context, R.color.black));
                        }
                    }
                }
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getItemType();
    }

    public class DayViewHolder extends RecyclerView.ViewHolder {
        public TextView tvDay;
        public TextView tvLunar;
        private LinearLayout itemDay;

        public DayViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDay = itemView.findViewById(R.id.tv_day);
            tvLunar = itemView.findViewById(R.id.tv_lunar);
            itemDay = itemView.findViewById(R.id.item_day);
        }
    }

    public class MonthViewHolder extends RecyclerView.ViewHolder {
        public TextView tvMonth;


        public MonthViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMonth = itemView.findViewById(R.id.tv_month);

        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
}